package com.gitee.mrj.aiboot.core.sf.generators.ui.widget;

import com.fasterxml.jackson.databind.node.ObjectNode;
import com.gitee.mrj.aiboot.annotation.config.KV;
import com.gitee.mrj.aiboot.annotation.config.sf.ui.widget.SFDateWidget;
import com.gitee.mrj.aiboot.core.sf.generators.FormDefinitionGenerator;
import com.gitee.mrj.aiboot.core.sf.generators.UIForm;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;

@Component
public class SFDateWidgetGenerator implements FormDefinitionGenerator {
    @Override
    public void generate(UIForm root, Field field, UIForm node) {
        SFDateWidget widget = AnnotatedElementUtils.findMergedAnnotation(field, SFDateWidget.class);
        generate(node, widget);
    }

    private void generate(UIForm node, SFDateWidget widget) {
        ObjectNode ui = node.getUi();
        ui.put("widget", "date");
        ui.put("mode", widget.mode().mode());
        ui.put("size", widget.size().value());
        putString(ui, "placeholder", widget.placeholder());
        ui.put("inline", widget.inline());
        putString(ui, "format", widget.format());
        putString(ui, "displayFormat", widget.displayFormat());
        putString(ui, "end", widget.end());
        ui.put("allowClear", widget.allowClear());
        putString(ui, "className", widget.className());
        if (ArrayUtils.isNotEmpty(widget.popupStyle())) {
            ObjectNode objectNode = putObject(ui, "popupStyle");
            for (KV kv : widget.popupStyle()) {
                objectNode.put(kv.key(), kv.value());
            }
        }
        putString(ui, "dropdownClassName", widget.dropdownClassName());
        ui.put("rangeMode", widget.rangeMode().value());
        putString(ui, "renderExtraFooter", widget.renderExtraFooter());
        ui.put("showTime", widget.showTime());
        ui.put("showToday", widget.showToday());
        ui.put("inputReadOnly", widget.inputReadOnly());
    }


    @Override
    public Class[] supports() {
        return new Class[]{SFDateWidget.class};
    }
}
